import 'package:flutter/material.dart';
import 'personal_verification_screen.dart';

class IdentityVerificationScreen extends StatefulWidget {
  const IdentityVerificationScreen({super.key});

  @override
  State<IdentityVerificationScreen> createState() => _IdentityVerificationScreenState();
}

class _IdentityVerificationScreenState extends State<IdentityVerificationScreen> {
  bool _isVerified = false;
  String _verificationStatus = '未认证';
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F7FA),
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Color(0xFF333333)),
          onPressed: () => Navigator.pop(context),
        ),
        title: const Text(
          '实名认证',
          style: TextStyle(
            color: Color(0xFF333333),
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 认证状态
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(16),
                boxShadow: const [
                  BoxShadow(
                    color: Color(0x1A000000),
                    blurRadius: 20,
                    offset: Offset(0, 4),
                  ),
                ],
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '认证状态',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF333333),
                    ),
                  ),
                  const SizedBox(height: 16),
                  Row(
                    children: [
                      Container(
                        width: 50,
                        height: 50,
                        decoration: BoxDecoration(
                          color: _isVerified 
                              ? const Color(0xFF52C41A).withOpacity(0.1)
                              : const Color(0xFFFAAD14).withOpacity(0.1),
                          borderRadius: BorderRadius.circular(25),
                        ),
                        child: Icon(
                          _isVerified ? Icons.verified_user : Icons.pending,
                          color: _isVerified 
                              ? const Color(0xFF52C41A)
                              : const Color(0xFFFAAD14),
                          size: 24,
                        ),
                      ),
                      const SizedBox(width: 16),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              _verificationStatus,
                              style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.w600,
                                color: _isVerified 
                                    ? const Color(0xFF52C41A)
                                    : const Color(0xFFFAAD14),
                              ),
                            ),
                            const SizedBox(height: 4),
                            Text(
                              _isVerified 
                                  ? '您已完成实名认证，可享受完整服务'
                                  : '完成实名认证后可享受更多服务',
                              style: TextStyle(
                                fontSize: 12,
                                color: Colors.grey[600],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  if (!_isVerified) ...[
                    const SizedBox(height: 20),
                    SizedBox(
                      width: double.infinity,
                      child: ElevatedButton(
                        onPressed: () => _startVerification(),
                        style: ElevatedButton.styleFrom(
                          backgroundColor: const Color(0xFF1890FF),
                          foregroundColor: Colors.white,
                          padding: const EdgeInsets.symmetric(vertical: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                        ),
                        child: const Text(
                          '开始认证',
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                  ],
                ],
              ),
            ),
            const SizedBox(height: 20),
            
            // 认证须知
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(16),
                boxShadow: const [
                  BoxShadow(
                    color: Color(0x1A000000),
                    blurRadius: 20,
                    offset: Offset(0, 4),
                  ),
                ],
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '认证须知',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF333333),
                    ),
                  ),
                  const SizedBox(height: 16),
                  _buildNoticeItem(
                    '1. 请确保提供的身份信息真实有效',
                    Icons.info_outline,
                    const Color(0xFF1890FF),
                  ),
                  _buildNoticeItem(
                    '2. 上传的证件照片需清晰完整',
                    Icons.photo_camera,
                    const Color(0xFF52C41A),
                  ),
                  _buildNoticeItem(
                    '3. 认证信息一旦提交不可修改',
                    Icons.lock_outline,
                    const Color(0xFFFAAD14),
                  ),
                  _buildNoticeItem(
                    '4. 认证审核需要1-3个工作日',
                    Icons.schedule,
                    const Color(0xFF722ED1),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 20),
            
            // 认证类型
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(16),
                boxShadow: const [
                  BoxShadow(
                    color: Color(0x1A000000),
                    blurRadius: 20,
                    offset: Offset(0, 4),
                  ),
                ],
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '认证类型',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF333333),
                    ),
                  ),
                  const SizedBox(height: 16),
                  _buildVerificationTypeCard(
                    '个人认证',
                    '适用于个人用户',
                    Icons.person,
                    const Color(0xFF1890FF),
                    ['身份证正反面', '人脸识别', '手机号验证'],
                    onTap: () => _showPersonalVerification(),
                  ),
                  const SizedBox(height: 16),
                  _buildVerificationTypeCard(
                    '企业认证',
                    '适用于企业用户',
                    Icons.business,
                    const Color(0xFF52C41A),
                    ['营业执照', '法人身份证', '授权委托书'],
                    onTap: () => _showEnterpriseVerification(),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 20),
            
            // 常见问题
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(16),
                boxShadow: const [
                  BoxShadow(
                    color: Color(0x1A000000),
                    blurRadius: 20,
                    offset: Offset(0, 4),
                  ),
                ],
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '常见问题',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF333333),
                    ),
                  ),
                  const SizedBox(height: 16),
                  _buildFAQItem(
                    '认证失败怎么办？',
                    '请检查上传的证件是否清晰完整，信息是否准确',
                  ),
                  _buildFAQItem(
                    '认证需要多长时间？',
                    '通常需要1-3个工作日，节假日可能延长',
                  ),
                  _buildFAQItem(
                    '可以修改认证信息吗？',
                    '认证信息提交后不可修改，请仔细核对',
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildNoticeItem(String text, IconData icon, Color color) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Icon(
            icon,
            size: 16,
            color: color,
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Text(
              text,
              style: TextStyle(
                fontSize: 14,
                color: Colors.grey[700],
                height: 1.4,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildVerificationTypeCard(
    String title,
    String subtitle,
    IconData icon,
    Color color,
    List<String> requirements, {
    VoidCallback? onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: color.withOpacity(0.05),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: color.withOpacity(0.2),
            width: 1,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Container(
                  width: 40,
                  height: 40,
                  decoration: BoxDecoration(
                    color: color.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Icon(
                    icon,
                    color: color,
                    size: 20,
                  ),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        title,
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: color,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        subtitle,
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey[600],
                        ),
                      ),
                    ],
                  ),
                ),
                Icon(
                  Icons.arrow_forward_ios,
                  size: 16,
                  color: Colors.grey[400],
                ),
              ],
            ),
            const SizedBox(height: 12),
            const Text(
              '需要材料：',
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.w500,
                color: Color(0xFF666666),
              ),
            ),
            const SizedBox(height: 8),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: requirements.map((requirement) => Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: color.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  requirement,
                  style: TextStyle(
                    fontSize: 10,
                    color: color,
                  ),
                ),
              )).toList(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFAQItem(String question, String answer) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            question,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Color(0xFF333333),
            ),
          ),
          const SizedBox(height: 8),
          Text(
            answer,
            style: TextStyle(
              fontSize: 12,
              color: Colors.grey[600],
              height: 1.4,
            ),
          ),
        ],
      ),
    );
  }

  void _startVerification() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('开始认证'),
        content: const Text('请选择认证类型：个人认证或企业认证'),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              _showPersonalVerification();
            },
            child: const Text('个人认证'),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              _showEnterpriseVerification();
            },
            child: const Text('企业认证'),
          ),
        ],
      ),
    );
  }

  void _showPersonalVerification() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => const PersonalVerificationScreen(),
      ),
    );
  }

  void _showEnterpriseVerification() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('企业认证'),
        content: const Text('企业认证需要提供：\n1. 营业执照\n2. 法人身份证\n3. 授权委托书\n\n请确保信息真实有效。'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              _simulateVerification();
            },
            child: const Text('开始认证'),
          ),
        ],
      ),
    );
  }

  void _simulateVerification() {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => const AlertDialog(
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            CircularProgressIndicator(),
            SizedBox(height: 16),
            Text('正在提交认证信息...'),
          ],
        ),
      ),
    );

    Future.delayed(const Duration(seconds: 2), () {
      Navigator.pop(context);
      setState(() {
        _verificationStatus = '审核中';
      });
      
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('提交成功'),
          content: const Text('您的认证信息已提交，我们将在1-3个工作日内完成审核。'),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('确定'),
            ),
          ],
        ),
      );
    });
  }
}